
function convertJbeiToTeselagen (seq) {
  return seq;
}

const comData = JSON.parse(sessionStorage.getItem('comData'))

const setRightClickMenu = (items) => {
  if (comData.type !== 'protein' && comData.type !== 'PROTEIN') {
    return items
  }
  items.forEach((item) => {
    if (item.text === 'Copy') {
      item.submenu = item.submenu.filter((ite) => {
        const { text } = ite
        return text !== 'Copy Reverse Complement DNA Bps' && text !== 'Copy Complement DNA Bps'
      })
    }
  })
  return items
}

const pluginMethods = (customizeProps, isFullscreen, setIsFullscreen, headMenusJson, rightClickMenusJson, versionArr, saveCallBackFn) => {
  return {
    menuFilter: (menuDef) => {
      return [...menuDef]
    },
    rightClickOverrides: { // 被右键单击时发生的情况 覆盖当给定的特征/部分/底漆/翻译/orf/cutsite/selectionLayer/lineageLine 
      selectionLayerRightClicked: (items, { annotation }, props) => {
        let menus = items || rightClickMenusJson
        return setRightClickMenu(menus);
      },
      partRightClicked: (items, { annotation }, props) => {
        return setRightClickMenu(items);
      },
      featureRightClicked: (items, { annotation }, props) => {
        return setRightClickMenu(items);
      },
      primerRightClicked: (items, { annotation }, props) => {
        return setRightClickMenu(items);
      }
    },
    // 进行任何更改后触发 onSave 回调
    onSave: function (opts, sequenceDataToSave, editorState, onSuccessCallback) {
      saveCallBackFn({
        opts,
        sequenceDataToSave,
        editorState
      }, onSuccessCallback)
    },
    onDelete: (data) => { // 删除
      console.warn("React-> would delete", data);
    },
    // 复制的
    onCopy: function (event, copiedSequenceData, editorState) {
      const oveData = JSON.parse(sessionStorage.getItem('oveData'))
      const clipboardData = event.clipboardData;
      const copyData = {
        ...copiedSequenceData,
        ...oveData,
        copySelectionRange: {
          start: editorState?.selectionLayer?.start || 0,
          end: editorState?.selectionLayer?.end
        },
        partid: comData?.code || null
      }

      console.log('copyData', JSON.stringify(copyData));

      clipboardData.setData(
        "application/json",
        JSON.stringify(copyData)
      );
      event.preventDefault();
    },
    // 粘贴
    onPaste: function (event, editorState) {
      const clipboardData = event.clipboardData;
      let jsonData = clipboardData.getData("application/json")
      // console.log('React -> [onPaste] jsonData :>> ', JSON.parse(jsonData));

      if (jsonData) {
        jsonData = JSON.parse(jsonData)
        if (jsonData.isJbeiSeq) {
          jsonData = convertJbeiToTeselagen(jsonData)
        }
      }
      const sequenceData = jsonData || {
        sequence: clipboardData.getData("text/plain")
      }
      return sequenceData
    },
    // 获取序列
    getSequenceAtVersion: (versionId) => {
      let data = {}
      for (let i = 0, len = versionArr.length; i < len; i++) {
        if (versionArr[i].versionId === versionId) {
          return versionArr[i].versionData
        }
      }
      if (data) return data
      else return {}
    },
    getVersionList: () => {
      return versionArr;
    }
  }
}

export default pluginMethods